const db = require( '../db/db' );
const redisUtils = require('../utils/redis_utils');
const tableName = 'core.setting';
const componentConfKey = 'app.component.conf';

/**
 * 获取所有设置
 */
let getAll = async function () {

    var sql = `select * from ${ tableName }`;

    try {
        var result = await db.runSql( sql, null );
        if ( result ) {
            return result.rows;
        }
        return null;
    } catch ( e ) {
        console.log( '查询系统设置信息出错', e );
    }

}

/**
 * 获取平台模块是否激活配置
 * @return {object / null}
 */
let getComponentConf = async function() {

    // 缓存中有相应数据则直接返回
    let cachedComponentConf = await redisUtils.get(componentConfKey);
    if (cachedComponentConf) {
        return cachedComponentConf;
    }

    let sql = `SELECT value FROM ${tableName} WHERE key = '${componentConfKey}'`;
    try {
        let result = await db.runSql(sql, null);
        if (result && result.rows[0]) {
            let componentConf = result.rows[0].value;
            redisUtils.set(componentConfKey, componentConf);
            return componentConf;
        }
        return null;
    } catch (error) {
        console.log('查询系统模块配置出错', e);
    }
}

exports.getAll = getAll;
exports.getComponentConf = getComponentConf;
